<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/10/15
 * Time: 15:29
 */
namespace app\admin\controller;
use app\admin\service\FinanceService;
use app\admin\service\RefundService;
use app\models\AllocationCampusModel;
use app\models\AllocationOnlyModel;
use app\models\CampusModel;
use app\models\CancelDeductionModel;
use app\models\CapitalAllocationRecordModel;
use app\models\CapitalRecordListModel;
use app\models\ContractModel;
use app\models\ContractModificationModel;
use app\models\CourseModel;
use app\models\DictionaryModel;
use app\models\GoodsContractModel;
use app\models\GradesDetailsModel;
use app\models\ReceivablesModel;
use app\models\RefundAmountModel;
use app\models\StudentSubjectInfosClassModel;
use app\models\UserModel;

/**
 * Class Finance
 * @package app\admin\controller
 */
class Finance extends Common
{

    /**
     * 班级管理
     * @var GradesDetailsModel
     */
    protected $gradesdetailModel;

	/**
	 * 退款逻辑
	 * @var RefundService
	 */
	protected $refundService;

    /**
     * 用户管理模型.
     *
     * @var UserModel
     */
    protected $userModel;

    /**
     * ReceivablesModel 资金流水管理模型.
     *
     * @var ReceivablesModel
     */
    protected $receivablesModel;

    /**
     * DictionaryModel 字典管理模型.
     *
     * @var DictionaryModel
     */
    protected $dictionaryModel;

    /**
     * CancelDeductionModel 取消扣费记录管理模型.
     *
     * @var CancelDeductionModel
     */
    protected $canceldeductionModel;

    /**
     * ContractModel 订单管理模型.
     *
     * @var ContractModel
     */
    protected $contractModel;

    /**
     * 学生分班排课管理模型.
     *
     * @var StudentSubjectInfosClassModel
     */
    protected $studentsubjectinfosclassModel;

    /**
     * RefundAmountModel 退款订单管理模型.
     *
     * @var RefundAmountModel
     */
    protected $refundamountModel;

    /**
     * CampusModel 校区管理模型.
     *
     * @var CampusModel
     */
    protected $campusModel;

    /**
     * AllocationCampusModel 校区业绩分配管理模型.
     *
     * @var AllocationCampusModel
     */
    protected $allocationcampusModel;

    /**
     * AllocationOnlyModel 个人业绩分配管理模型.
     *
     * @var AllocationOnlyModel
     */
    protected $allocationonlyModel;

    /**
     * 资金回抽分配记录管理模型.
     *
     * @var CapitalRecordListModel
     */
    protected $capitalrecordlistmodel;

    /**
     * 课程模型.
     *
     * @var CourseModel
     */
    protected $courseModel;
    /**
     * 资金分配管理模型.
     *
     * @var CapitalAllocationRecordModel
     */
    protected $capitalallocationrecordModel;

    /**
     * 订单修改模型.
     *
     * @var ContractModificationModel
     */
    protected $contractmodificationmodel;

    /**
     * 物品管理模型.
     *
     * @var GoodsContractModel
     */
    protected $goodsContractModel;

    /**
     * 已报课程
     * @return \think\response\Json|\think\response\View
     */
    public function already_reported_course()
    {
        if (request()->isPost()) {
            if(input('type') == 1){
                $param = $this->request->param();
                $result = $this->goodsContractModel->getGoodsContractLists($param);
                $this->success('success', '', $result, 0);
            }else{
                return json((new FinanceService())->already_reported_course());
            }

        }
		$where = [];
		$campus_where = [];
		if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
			$where['campus_id'] = get_current_campus_id();
			$campus_where['id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
			$where['campus_id'] = ['in',get_current_campus_id()];
			$campus_where['id'] = ['in',get_current_campus_id()];
		}
        $grades_list = $this->gradesdetailModel->where($where)->select();
		$where['status'] = 1;
		$campus_where['status'] = 1;
        $campus = $this->campusModel->where($campus_where)->select()->toArray();
        $course_list = $this->courseModel->where($where)->select();

        return $this->fetch('',['grades_list' => $grades_list, 'course_list' => $course_list,'campus' => $campus]);
    }

    /**
     * 收款管理
     * @return \think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function receivables_management()
    {
        if (request()->isPost()) {
            return json((new FinanceService())->receivables_management());
        }
        $this->dictionary();
		$campus_where = [];
		if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
			$campus_where['id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
			$campus_where['id'] = ['in',get_current_campus_id()];
		}
		$campus_where['status'] = 1;
		$campus = $this->campusModel->where($campus_where)->select()->toArray();
        return  $this->fetch('',['campus' => $campus]);;
    }

	/**
	 * 支付凭证
	 * User: wufeng
	 * Date: 2020/03/24.
	 *
	 * @throws \think\exception\DbException
	 *
	 * @return mixed
	 */
	public function payment_voucher()
	{
		$receivables = (new ReceivablesModel())->get(input('id'));
		$picture = explode(',',$receivables['picture']);
		return $this->fetch('', [
			'receivables' => $receivables,
			'picture' => $picture,
		]);
	}

	/**
	 * 退款
	 * User: wufeng
	 * Date: 2019/4/17
	 *
	 */
	public function refund()
	{
		if ($this->request->isAjax() && $this->request->isPost()) {
			$param = $this->request->param();
			$refund = $this->refundService->refund($param);
			if('0000' == $refund['code']){
				$this->success('退款成功','','',1);
			}
			$this->error($refund['msg'],'','',1);
		}
		$receipt_number = $this->receivablesModel->get(['receipt_number'=>input('receipt_number')]);
		$receipt_number['refund_voucher'] = 'TK'.date("Ymd").time();
		$receipt_number['amount'] = $receipt_number['amount'] / 100;
		return $this->fetch('', [
			'receipt_number' => $receipt_number,
		]);
	}

    /**
     * @return \think\response\Json
     */
    public function receivables_check_status()
    {
        if (request()->isPost()) {
            return json((new FinanceService())->receivables_check_status());
        }
    }

    /**
     * 业绩分配记录
     */
    public function performance_allocation_record()
    {
		$campus_where = [];
		if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
			$campus_where['id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
			$campus_where['id'] = ['in',get_current_campus_id()];
		}
		$campus_where['status'] = 1;
        $campus_name = $this->campusModel->where($campus_where)->select();
        $this->dictionary();
        return $this->fetch('',['campus_name' => $campus_name]);
    }

    /**
     * 资金分配记录
     */
    public function capital_allocation_record()
    {

        if ($this->request->isAjax()){
            $param = $this->request->param();
            $result = $this->capitalrecordlistmodel->getCapitalRecordAllListLists($param);
            $this->success('success', '', $result, 0);
        }
		$campus_where = [];
		if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
			$campus_where['id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
			$campus_where['id'] = ['in',get_current_campus_id()];
		}
		$campus_where['status'] = 1;
        $campus_name = $this->campusModel->where($campus_where)->select();
        return $this->fetch('',['campus_name' => $campus_name]);
    }

    /**
     * 订单修改记录.
     *
     * @throws \think\exception\DbException
     *
     * @return mixed
     */
    public function contract_modification_record()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $result = $this->contractmodificationmodel->getContractModfifyLists($param);
            $this->success('success', '', $result, 0);
        }

        return $this->fetch();
    }

    /**
     * 财务汇总
     */
    public function financial_summary()
    {
        $result = $this->dictionaryModel->where(['type' => 20])->select();
        $data = [];
        foreach ($result as $key => $value){
            $item = [];
            $money = $this->receivablesModel->where(['payment_method' => $value['id']])->sum('amount');
            $item['value'] = ($money / 100);
            $item['name'] = $value['name'];
            $data[] = $item;
        }
        $data = json_encode($data);
        $this->dictionary();
		$contract_where = [];
		$receivables_where = [];
        $receivables_where['status'] = 2; //已经审核通过
		//统计报表
		if(get_current_type() == 4|| get_current_type() == 5){/*校区数据权限*/
			$contract_where['campus_id'] = get_current_campus_id();
            $receivables_where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
			$contract_where['campus_id'] = ['in',get_current_campus_id()];
            $receivables_where['campus_id'] = ['in',get_current_campus_id()];
		}
        $total_amount = ($this->contractModel->where($contract_where)->sum('total_amount') / 100);  //应收
        $amount = ($this->receivablesModel->where($receivables_where)->sum('amount') / 100);  //实收
        $net_income = ($this->studentsubjectinfosclassModel->where($contract_where)->where(['charging' => 1])->sum('amount_deducted') / 100); //净收入

        //校区业绩报表
        $data_campus = [];

		$campus_where = [];
		if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
			$campus_where['id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
			$campus_where['id'] = ['in',get_current_campus_id()];
		}
		$campus_where['status'] = 1;
		$campus_name = $this->campusModel->where($campus_where)->select();
        foreach ($campus_name as $key => $value){
            $item = [];
            $campus_money = $this->allocationcampusModel->where(['campus_id_all' => $value['id']])->sum('rate_all_money'); //校区业绩
            $item['value'] = ($campus_money / 100);
            $item['campus_name'] = $value['campus_name'];
            $data_campus[] = $item;
        }
		$rate_all_money_where = [];
		if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
			$rate_all_money_where['campus_id_all'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
			$rate_all_money_where['campus_id_all'] = ['in',get_current_campus_id()];
		}
        $campus_money_sum = ($this->allocationcampusModel->where($rate_all_money_where)->sum('rate_all_money') / 100);
        $data_campus = json_encode($data_campus);
		

        //个人业绩报表

        /********分配人************/
        $user_where = [];
        $user_where['status'] = 1;
        if(get_current_type() == 4 || get_current_type() == 5){
            $user_where['campus_id'] = get_current_campus_id();
        }elseif (get_current_type() == 2 || get_current_type() == 3){
            $user_where['campus_id'] = ['in',get_current_campus_id()];
        }
        $user_result = $this->userModel->where($user_where)->select();

        $data_only = [];
        /*
        $only_name = $this->dictionaryModel->where(['type' => 25])->select();
        */
        foreach ($user_result as $key => $value){
            $item = [];
            $only_money = $this->allocationonlyModel->where(['payee_only' => $value['id']])->sum('rate_only_money'); //个人业绩
            $item['value'] = ($only_money / 100);
            $item['only_name'] = $value['name'];
            $data_only[] = $item;
        }
		$rate_only_money_where = [];
		if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
			$rate_only_money_where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
			$rate_only_money_where['campus_id'] = ['in',get_current_campus_id()];
		}
        $only_money_sum = ($this->allocationonlyModel->where($rate_only_money_where)->sum('rate_only_money') / 100);
        $data_only = json_encode($data_only);


        //退费报表
        $data_refund_campus = [];
        $campus_refund_name = $this->campusModel->where($campus_where)->select();
        foreach ($campus_refund_name as $key => $value){
            $item = [];
            $campus_refund_money = $this->refundamountModel->where(['campus_id' => $value['id']])->sum('refund_amount'); //退费金额
            $item['refund_value'] = ($campus_refund_money / 100);
            $item['refund_campus_name'] = $value['campus_name'];
            $data_refund_campus[] = $item;
        }
		$refundamount_where = [];
		if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
			$refundamount_where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
			$refundamount_where['campus_id'] = ['in',get_current_campus_id()];
		}
        $campus_refund_money_sum = ($this->refundamountModel->where($refundamount_where)->sum('refund_amount') / 100);
        $data_refund_campus = json_encode($data_refund_campus);





        return $this->fetch('',['user' => $user_result, 'jsondata' => $data, 'data_refund_campus' => $data_refund_campus,  'campus_refund_money_sum' => $campus_refund_money_sum,  'data_only' => $data_only, 'only_money_sum' =>$only_money_sum ,'campus_name' => $campus_name, 'campus_money_sum' => $campus_money_sum,  'data_campus' => $data_campus,  'total_amount' => $total_amount, 'amount' => $amount, 'net_income' => $net_income]);
    }

    /***
    *
     支付类型
     */
    public function pay_type(){
        if ($this->request->isAjax()){
            $param = $this->request->param();
            $result = $this->dictionaryModel->getPayTypeyLists($param);
            $this->success('success', '', $result, 0);
        }
    }

    /**
     * 资金流水
     * @return \think\response\Json|\think\response\View
     */
    public function capital_flow()
    {
        if (request()->isPost()) {
            return json((new FinanceService())->capital_flow());
        }
		$campus_where = [];
		if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
			$campus_where['id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
			$campus_where['id'] = ['in',get_current_campus_id()];
		}
		$campus_where['status'] = 1;
		$campus_name = $this->campusModel->where($campus_where)->select();
        $this->dictionary();
        return $this->fetch('',['campus_name' => $campus_name]);
    }

    /**
     * 扣费记录
     */
    public function deduction_record()
    {
        return view();
    }

    /**
     * 退费记录
     */
    public function refund_record()
    {
		$campus_where = [];
		if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
			$campus_where['id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
			$campus_where['id'] = ['in',get_current_campus_id()];
		}
		$campus_where['status'] = 1;
		$campus_name = $this->campusModel->where($campus_where)->select();
        $this->dictionary();
        return $this->fetch('',['campus_name' => $campus_name]);
    }

    /**
    *退费记录
     */
    public function refundlist(){
        if ($this->request->isAjax()) {
            $status = false;
            if(input('status')){
                $status = true;
            }else{
                $status = false;
            }
            $param = $this->request->param();
            $course =  $this->refundamountModel->getrefundlist($param,$status);
            $this->success('success', '', $course, 0);
        }

        return $this->fetch();
    }

    /**
        删除退费记录
     */
    public function del(){
        if ($this->request->isAjax() && $this->request->isPost()) {
            if(!$this->refundamountModel->where(['id' => input('id')])->update(['status' => 1])){
                $this->error('删除退费记录失败', null, '', 1);
            }
            $this->success('删除退费记录成功', '', '', 1);
        }
    }


    /**
     * 扣费取消记录
     */
    public function deduction_cancel_record()
    {
        return view();
    }

    /**
     * 电子商务流水
     */
    public function commerce_flow()
    {
        return view();
    }

    /**校区业绩*/
    public function campuslists(){

        if ($this->request->isAjax()) {
            $status = false;
            if(input('status')){
                $status = true;
            }else{
                $status = false;
            }
            $param = $this->request->param();
            $campus_allocation = $this->allocationcampusModel->getCampusAllocationLists($param,$status);
            $this->success('success', '', $campus_allocation, 0);
        }

    }
    /**个人业绩*/
    public function onlylists(){

        if ($this->request->isAjax()) {
            $status = false;
            if(input('status')){
                $status = true;
            }else{
                $status = false;
            }
            $param = $this->request->param();
            $campus_allocation = $this->allocationonlyModel->getOnlyAllocationLists($param,$status);
            $this->success('success', '', $campus_allocation, 0);
        }

    }

    /**
     * 删除校区业绩数据
     *
     * @return mixed
     */
    public function campus_del()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {

            if (!$this->allocationcampusModel->where(['id'=>input('id')])->delete()) {
                $this->error('删除校区业绩数据失败', null, '', 1);
            }
            $this->success('删除校区业绩数据成功', '', '', 1);
        }
    }

    /**
     * 删除个人业绩数据
     *
     * @return mixed
     */
    public function only_del()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {

            if (!$this->allocationonlyModel->where(['id'=>input('id')])->delete()) {
                $this->error('删除个人业绩数据失败', null, '', 1);
            }
            $this->success('删除个人业绩数据成功', '', '', 1);
        }
    }

    /**校区业绩调整*/
    public function performance_all(){
        $id = input('id');  //id
        $res = $this->allocationcampusModel->get(['id' => $id]);

        $where = [];
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['id'] = ['in',get_current_campus_id()];
		}
        $campus = $this->campusModel->where($where)->select()->toArray();
        $this->dictionary();

        return $this->fetch('',['campus' => $campus, 'res' => $res, 'id' => $id]);
    }


    /**个人业绩调整*/
    public function performance_only(){
        $id = input('id');  //id
        $res = $this->allocationonlyModel->get(['id' => $id]);
        $this->dictionary();

        /********分配人************/
        $allocation_where = [];
        $allocation_where['status'] = 1;
        if(get_current_type() == 4 || get_current_type() == 5){
            $allocation_where['campus_id'] = get_current_campus_id();
        }elseif (get_current_type() == 2 || get_current_type() == 3){
            $allocation_where['campus_id'] = ['in',get_current_campus_id()];
        }
        $allocation_result = $this->userModel->where($allocation_where)->select();

        return $this->fetch('',['res' => $res,'allocation_user' => $allocation_result, 'id' => $id]);
    }


    /***保存校区业绩**/
    public function save_all(){
        if ($this->request->isAjax() && $this->request->isPost()) {
                $data = input('post.');
                $save_data['id'] = $data['id'];
                $save_data['rate_all_money']  = $data['rate_all_money'] * 100;
                $save_data['rate_all'] = $data['rate_all'];
                $save_data['campus_id_all'] = $data['campus_id_all'];
                if(!$this->allocationcampusModel->save($save_data,['id' => $data['id']])){
                    $this->error('调整校区业绩数据失败', null, '', 1);
                }
            $this->success('调整校区业绩数据成功', '', '', 1);
        }
    }

    /***保存校区业绩**/
    public function save_only(){
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $save_data['id'] = $data['id'];
            $save_data['rate_only_money']  = $data['rate_only_money'] * 100;
            $save_data['rate_only'] = $data['rate_only'];
			$save_data['campus_id'] = $this->allocationonlyModel->get(['id' => $data['id']])['campus_id'];
            if(!$this->allocationonlyModel->save($save_data,['id' => $data['id']])){
                $this->error('调整个人业绩数据失败', null, '', 1);
            }
            $this->success('调整个人业绩数据成功', '', '', 1);
        }
    }


    /**扣费记录*/
    public function getDeductionLists(){

        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $deduction_lists = $this->studentsubjectinfosclassModel->getDeductionRecordLists($param);
            $this->success('success', '', $deduction_lists, 0);
        }

    }


    /*****取消扣费*********/
    public function cancel()
	{
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $id = $data['id'];
            $cancel_describe = $data['cancel_describe']; //取消原因
            $res= $this->studentsubjectinfosclassModel->get(['id' => $id]);
            $contract_number = $res['contract_number']; //订单编号
            $res_contract = $this->contractModel->get(['contract_number' => $contract_number]);
            $res_contract['balance'] =  $res_contract['balance'] + $res['amount_deducted']; // 更新账户余额
            $res_contract['expenditure'] = $res_contract['expenditure'] - $res['amount_deducted']; //支出金额

            $update_data['amount_deducted'] = 0;
            $update_data['charging'] = 0;

            $cancel_table['infos_id'] = $id; //取消ID
            $cancel_table['campus_id'] = $data['campus_id'];
            $cancel_table['cancel_reason'] = $cancel_describe; //取消原因
            $cancel_table['amount_deducted'] = $res['amount_deducted'];//取消扣费金额
            $cancel_table['student_name'] = $res['student_name'];//学员姓名
            //操作人
            $cancel_table['operatorer'] = get_current_name();  //操作人

            if(!$this->canceldeductionModel->save($cancel_table)){
                $this->error('插入取消扣费记录表失败', null, '', 1);
            }

            if(!$this->studentsubjectinfosclassModel->save($update_data,['id' => $id])){
                $this->error('更新节课信息失败', null, '', 1);
            }

            $this->contractModel->where(['id'=> $res_contract['id']])->update(['balance' => $res_contract['balance'], 'expenditure' => $res_contract['expenditure']]);


            $this->success('取消扣费成功', '', '', 0);
        }

    }


    /*****扣费取消记录*********/
    public function getcancelDeductionLists(){
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $deduction_lists = $this->canceldeductionModel->getCancelDeductionLists($param);
            $this->success('success', '', $deduction_lists, 0);
        }

    }

    //查询扣费记录
    public function searchdata(){
        if ($this->request->isAjax()) {
            $type = input('type');
            $return_data = [];
            $data = '';
            //校区业绩
            if($type == 2){
                $data =  input('post.');
                $where = [];
                $where_campus = [];
                $where_ = [];
                if (isset($data['campus_id_all_text']) && '' !== $data['campus_id_all_text']) {
                    $where['campus_id_all_text'] = ['like', '%'.$data['campus_id_all_text'].'%'];
                    $where_campus['campus_name'] = ['like', '%'.$data['campus_id_all_text'].'%'];
                }
                //时间段
                if (isset($data['start_date']) && '' !== $data['start_date'] && isset($data['end_date']) && '' !== $data['end_date']) {
                    $where['update_time'] = array('between', array($data['start_date'].' 00:00:00',$data['end_date'].' 23:59:59'));
                    $where_['update_time'] = array('between', array($data['start_date'].' 00:00:00',$data['end_date'].' 23:59:59'));
                }

                //校区业绩报表
                $data_campus = [];
				if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
					$where_campus['id'] = get_current_campus_id();
				}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
					$where_campus['id'] = ['in',get_current_campus_id()];
				}
                $campus_name = $this->campusModel->where($where_campus)->select();
                foreach ($campus_name as $key => $value){
                    $item = [];
                    $where_['campus_id_all'] = $value['id'];
                    $campus_money = $this->allocationcampusModel->where($where_)->sum('rate_all_money'); //校区业绩

                    if(count($where_) == 2){
                        if($campus_money){
                            $item['value'] = ($campus_money / 100);
                            $item['campus_name'] = $value['campus_name'];
                            $data_campus[] = $item;
                        }
                    }else{
                        $item['value'] = ($campus_money / 100);
                        $item['campus_name'] = $value['campus_name'];
                        $data_campus[] = $item;
                    }



                }
                $campus_money_sum = ($this->allocationcampusModel->where($where)->sum('rate_all_money') / 100);


                $return_data['campus_sum'] = $campus_money_sum;
                $return_data['campus_json'] = $data_campus;
                $return_data = json_encode($return_data);

            }else if($type == 3){
                $data =  input('post.');
                $where = [];
                $where_only= ['type' => 25];
                $where_ = [];
                if (isset($data['payee_only_text']) && '' !== $data['payee_only_text']) {
                    $where['payee_only_text'] = ['like', '%'.$data['payee_only_text'].'%'];
                    $where_only['name'] = ['like', '%'.$data['payee_only_text'].'%'];
                }
                //时间段
                if (isset($data['start_date']) && '' !== $data['start_date'] && isset($data['end_date']) && '' !== $data['end_date']) {
                    $where['update_time'] = array('between', array($data['start_date'].' 00:00:00',$data['end_date'].' 23:59:59'));
                    $where_['update_time'] = array('between', array($data['start_date'].' 00:00:00',$data['end_date'].' 23:59:59'));
                }

                //个人业绩报表
                $data_only = [];
                $only_name = $this->dictionaryModel->where($where_only)->select();
                foreach ($only_name as $key => $value){
                    $item = [];
                    $where_['payee_only'] = $value['id'];
                    $only_money = $this->allocationonlyModel->where($where_)->sum('rate_only_money'); //个人业绩

                    if(count($where_) == 2){
                        if($only_money){
                            $item['value'] = ($only_money / 100);
                            $item['only_name'] = $value['name'];
                            $data_only[] = $item;
                        }
                    }else{
                        $item['value'] = ($only_money / 100);
                        $item['only_name'] = $value['name'];
                        $data_only[] = $item;
                    }


                }
                $only_money_sum = ($this->allocationonlyModel->where($where)->sum('rate_only_money') / 100);

                $return_data['only_sum'] = $only_money_sum;
                $return_data['only_json'] = $data_only;
                $return_data = json_encode($return_data);

            }else if($type == 4){
                $data =  input('post.');
                $where = [];
                $where_campus = [];
                $where_ = [];
                if (isset($data['campus_name']) && '' !== $data['campus_name']) {
                    $where['campus_name'] = ['like', '%'.$data['campus_name'].'%'];
                    $where_campus['campus_name'] = ['like', '%'.$data['campus_name'].'%'];
                }
                //时间段
                if (isset($data['start_date']) && '' !== $data['start_date'] && isset($data['end_date']) && '' !== $data['end_date']) {
                    $where['update_time'] = array('between', array($data['start_date'].' 00:00:00',$data['end_date'].' 23:59:59'));
                    $where_['update_time'] = array('between', array($data['start_date'].' 00:00:00',$data['end_date'].' 23:59:59'));
                }

                //退费报表
                $data_refund_campus = [];
				if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
					$where_campus['id'] = get_current_campus_id();
				}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
					$where_campus['id'] = ['in',get_current_campus_id()];
				}
                $campus_refund_name = $this->campusModel->where($where_campus)->select();
                foreach ($campus_refund_name as $key => $value){
                    $item = [];
                    $where_['campus_id'] = $value['id'];
                    $campus_refund_money = $this->refundamountModel->where($where_)->sum('refund_amount'); //退费金额

                    if(count($where_) == 2){
                        if($campus_refund_money){
                            $item['refund_value'] = ($campus_refund_money / 100);
                            $item['refund_campus_name'] = $value['campus_name'];
                            $data_refund_campus[] = $item;
                        }

                    }else{
                        $item['refund_value'] = ($campus_refund_money / 100);
                        $item['refund_campus_name'] = $value['campus_name'];
                        $data_refund_campus[] = $item;
                    }

                }

				if(get_current_type() == 4 || get_current_type() == 5){
					$where['campus_id'] = get_current_campus_id();
				}elseif (get_current_type() == 2 || get_current_type() == 3){
					$where['campus_id'] = ['in',get_current_campus_id()];
				}
                $campus_refund_money_sum = ($this->refundamountModel->where($where)->sum('refund_amount') / 100);

                $return_data['campus_refund_sum'] = $campus_refund_money_sum;
                $return_data['campus_refund_json'] = $data_refund_campus;
                $return_data = json_encode($return_data);


            }

            $this->success('success', '', $return_data, 0);
        }
    }


    /********查询校区业绩统计******/
    public function searchcampusstatistics(){
        if ($this->request->isAjax()) {
            $data =  input('post.');
            $return_data = [];
            $where = ['type' => 20];
            $where_ = [];

            //支付类型名称
            if (isset($data['name']) && '' !== $data['name']) {
                $where['name'] = ['like', '%'.$data['name'].'%'];
            }

            //时间段
            if (isset($data['start_date']) && '' !== $data['start_date'] && isset($data['end_date']) && '' !== $data['end_date']) {
                $where_['paydate'] = array('between', array($data['start_date'],$data['end_date']));
            }

            $result = $this->dictionaryModel->where($where)->select();
            $data_statistics = [];
            foreach ($result as $key => $value) {
                $item = [];
                $where_['payment_method'] = $value['id'];
				if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
					$where_['campus_id'] = get_current_campus_id();
				}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
					$where_['campus_id'] = ['in',get_current_campus_id()];
				}
                $money = $this->receivablesModel->where($where_)->sum('amount');
                $item['value'] = ($money / 100);
                $item['name'] = $value['name'];
                $data_statistics[] = $item;
            }
			$contract_where = [];
            //统计报表
			if(get_current_type() == 4 || get_current_type() == 5){/*校区数据权限*/
				$contract_where['campus_id'] = get_current_campus_id();
			}elseif (get_current_type() == 2 || get_current_type() == 3){/*区域数据权限*/
				$contract_where['campus_id'] = ['in',get_current_campus_id()];
			}
            $total_amount = ($this->contractModel->where($contract_where)->sum('total_amount') / 100);  //应收
            $amount = ($this->receivablesModel->where($contract_where)->sum('amount') / 100);  //实收
            $net_income = ($this->studentsubjectinfosclassModel->where($contract_where)->where(['charging' => 1])->sum('amount_deducted') / 100); //净收入

            $return_data['data_statistics'] =$data_statistics;
            $return_data['total_amount'] =$total_amount;
            $return_data['amount'] =$amount;
            $return_data['net_income'] =$net_income;

        }
    }

    /**
     *  实例化.
     */
    protected function _initialize()
    {
        parent::_initialize();
        $this->contractmodificationmodel = new ContractModificationModel();
        $this->capitalallocationrecordModel = new CapitalAllocationRecordModel();
        $this->capitalrecordlistmodel = new CapitalRecordListModel();
        $this->allocationcampusModel = new AllocationCampusModel();
        $this->allocationonlyModel = new AllocationOnlyModel();
        $this->campusModel = new CampusModel();
        $this->refundamountModel = new RefundAmountModel();
        $this->studentsubjectinfosclassModel = new StudentSubjectInfosClassModel();
        $this->contractModel = new ContractModel();
        $this->canceldeductionModel = new CancelDeductionModel();
        $this->dictionaryModel = new DictionaryModel();
        $this->receivablesModel = new ReceivablesModel();
        $this->courseModel  = new CourseModel();
        $this->userModel = new UserModel();
        $this->gradesdetailModel = new GradesDetailsModel();
        $this->refundService = new RefundService();
        $this->goodsContractModel = new GoodsContractModel();
    }
}